home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Telnet 2.6.1d1 4⁄26⁄94 Folder / source / init / init.c < prev    next >
C/C++ Source or Header  |  1994-04-16  |  13KB  |  463 lines

  1. /*
  2. *    init.c
  3. *    Top level of init code & general initialization routines
  4. *    Called only by main()
  5. *
  6. *****************************************************************
  7. *    NCSA Telnet for the Macintosh                                *
  8. *                                                                *
  9. *    National Center for Supercomputing Applications                *
  10. *    Software Development Group                                    *
  11. *    152 Computing Applications Building                            *
  12. *    605 E. Springfield Ave.                                        *
  13. *    Champaign, IL  61820                                        *
  14. *                                                                *
  15. *    Copyright (c) 1986-1992,                                    *
  16. *    Board of Trustees of the University of Illinois                *
  17. *****************************************************************
  18. *  Revisions:
  19. *  7/92        Telnet 2.6:    Initial version.  Jim Browne
  20. */
  21.  
  22. #ifdef MPW
  23. #pragma segment INIT
  24. #endif
  25.  
  26. #include "TelnetHeader.h"
  27.  
  28. #include <string.h>
  29. #include <Folders.h>
  30. #include <Script.h>
  31. #include <stdio.h>
  32. #include <GestaltEqu.h>
  33. #include <AppleEvents.h>
  34.  
  35. #include "init.proto.h"
  36. #include "debug.h"
  37. #include "mainseg.proto.h"        /* For forcequit() proto */
  38. #include "iNetwork.proto.h"    /* For initnet proto */
  39. #include "macros.proto.h"        /* For setmacro proto */
  40. #include "wind.h"        /* For WindRec definition */
  41. #include "rsmac.proto.h"        /* For RSgetwindow & RSnewwindow protos */
  42. #include "DlogUtils.proto.h"    /* For VersionNumber, UItemAssign, GetNewMyDialog protos */
  43. #include "Sets.proto.h"        // For readconfig proto
  44. #include "prefs.proto.h"
  45. #include "vr.h"            /* For VRinit proto */
  46. #include "vsdata.h"
  47. #include "vsinterf.proto.h"    /* For VSinit proto */
  48. #include "vgtek.proto.h"        /* For VGinit proto */
  49. #include "vrrgmac.proto.h"    /* For MacRGinit proto */
  50. #include "telneterrors.h"        /* for DoError proto */
  51. #include "tekrgmac.proto.h"    /* For RGMalloc proto */
  52. #include "menuseg.proto.h"
  53. #include "translate.proto.h"
  54. #include "dnr.proto.h"    // For OpenResolver proto
  55. #include "desproto.h"
  56. #include "ae.proto.h"            // For AppleEvent handlers
  57. #include "MacBinary.h"
  58. #include "binsubs.proto.h"        // For init_mb_files proto
  59.  
  60.  
  61. /*--------------------------------------------------------------------------------*/
  62. #define CurrentVersion 1                /* Last known SysEnvirons version */
  63. #define HFSPtr ((long *)1014)
  64.  
  65. extern    char *tempspot;                /* for temp storage only */
  66.  
  67. extern    unsigned char
  68.     *macro[10];        /* the wonderful macro package .... */
  69.  
  70. extern    Cursor *theCursors[];
  71.  
  72. extern    WindRec
  73.     *screens,        /* Window Records (VS) for :    Current Sessions */
  74.     *ftplog;            /*                                FTP Log             */
  75. extern    MenuHandle    myMenus[];
  76.  
  77. OSErr        io;
  78. char        *mungbuf;
  79. SysEnvRec    theWorld;                        /* System Environment record */
  80. short        TempItemsVRefNum;
  81. long        TempItemsDirID;
  82. Boolean        gKeyboardHasControlKey, gAEavail;
  83.  
  84. Boolean encryptOK;                // true if des encryption routines available
  85. Boolean authOK;                    // true if authentication driver available
  86.  
  87. /*    checkCONF - check if the user clicked on a configuration file */
  88. void checkCONF( void)
  89. {
  90.     short    i,mess,count;
  91.     AppFile    theFile;
  92.     FSSpec    FileSpec;
  93.     long    junk = 0;
  94.  
  95.     if (gAEavail) return;    // If AppleEvents are available, we'll get ODOCs
  96.  
  97. #ifndef __powerpc__
  98.     CountAppFiles(&mess,&count);
  99.     if (mess==1) return;
  100.     if (count<1) return;
  101.     for(i=1;i<=count;i++) {
  102.         GetAppFiles(i, &theFile);
  103.         if (theFile.fType=='CONF') {
  104.             ClrAppFiles(i);
  105.             BlockMove(&theFile.fName, FileSpec.name, (*theFile.fName)+1); // pstring copy theFile.fName -> FileSpec.name
  106.             GetWDInfo(theFile.vRefNum, &FileSpec.vRefNum, &FileSpec.parID, &junk);
  107.             readconfig(FileSpec);    /* BYU LSC */
  108.             }
  109.         }
  110. #endif
  111.     return;
  112. }
  113.  
  114. void initmacros( void)
  115. {
  116.     short i;
  117.  
  118.     for (i=0; i<10 ; i++) {
  119.         macro[i] = (unsigned char *) NewPtrClear(256);        /* BYU LSC */
  120.         if (macro[i]== NULL) 
  121.             {            /* Cant allocate space for the Macros */
  122.             DoError(102 | MEMORY_ERRORCLASS, LEVEL3,NULL);    
  123.             forcequit();
  124.             }
  125.         setmacro( i, (unsigned char *) "");        /* BYU LSC */
  126.         }
  127. }
  128.  
  129. void initEvents( void)
  130. {
  131.     EventRecord myEvent;
  132.  
  133.     FlushEvents(everyEvent - diskEvt,0);    /* Don't Let 'em get off without seeing it. */
  134.  
  135.     WaitNextEvent(everyEvent, &myEvent, gApplicationPrefs->TimeSlice, 0L);    /* WNE always avail */
  136.     WaitNextEvent(everyEvent, &myEvent, gApplicationPrefs->TimeSlice, 0L);
  137. }
  138.  
  139. /* initftplog - initialize the FTP log window */
  140. void initftplog( void)
  141. {
  142.     Rect prect;
  143.  
  144.     SetRect(&prect, 300,256,512,384);
  145.  
  146.     ftplog = (WindRec *) NewPtrClear(sizeof(WindRec));
  147.     
  148.     ftplog->vs=RSnewwindow( &prect, 50, 80, 24,
  149.                     "\pFTP Log", 1, DefFONT, DefSIZE, gFTPServerPrefs->ShowFTPlog,
  150.                         1,0);    /* NCSA 2.5 */
  151.  
  152.     ftplog->wind = RSgetwindow( ftplog->vs);
  153.     ((WindowPeek)ftplog->wind)->windowKind = WIN_LOG;
  154.     
  155.     VSwrite(ftplog->vs,"\033[24;0H",7);        /* Move to bottom of screen */
  156.     ftplog->active=0;                         /* Meaningless */
  157.     ftplog->port=0;                            /* As is this */
  158.     ftplog->termstate=VTEKTYPE;
  159. }
  160.  
  161.   /* copies a specified cursor from a resource into a nonrelocatable block
  162.     and returns a pointer to it in *result. */
  163. void loadcursor( short CursorID, CursPtr *result)
  164. {
  165.     CursHandle tempcurs;
  166.     OSErr Err;
  167.  
  168.     *result = nil;
  169.     do /* once */
  170.       {
  171.         tempcurs = GetCursor(CursorID);
  172.         Err = ResError();
  173.         if (Err != noErr)
  174.             break;
  175.         *result = (CursPtr) NewPtr(sizeof(Cursor));        /* BYU LSC */
  176.         Err = MemError();
  177.         if (Err != noErr)
  178.             break;
  179.         LoadResource((Handle) tempcurs); /* in case it was purged */
  180.         Err = ResError();
  181.         if (Err != noErr)
  182.             break;
  183.         BlockMove((Ptr) *tempcurs, (Ptr) *result, sizeof(Cursor));
  184.       }
  185.     while (false);
  186.     if (Err != noErr)
  187.       {
  188.         if (*result != nil)
  189.             DisposPtr((Ptr) *result);
  190.         *result = &qd.arrow;
  191.       }
  192. } /* loadcursor */
  193.  
  194. /* initmcurs - Set up the mouse Cursors
  195.  *        NOTE: debug tells if the bug or the pointer is to be used as dflt.*/
  196. void initmcurs ( short debug) 
  197. {
  198.     loadcursor(rWatchCursor, &theCursors[watchcurs]);
  199.     loadcursor(rCrossCursor, &theCursors[graphcurs]);
  200.     loadcursor(rPOSCURS, &theCursors[poscurs]);
  201.     loadcursor(rBeamCursor, &theCursors[textcurs]);
  202.     loadcursor(rGINCURS, &theCursors[gincurs]);
  203.     loadcursor(rXferCURS, &theCursors[xfercurs]);
  204.     if (debug)
  205.         loadcursor(rDbugCURS, &theCursors[normcurs]);
  206.     else
  207.         theCursors[normcurs] = &qd.arrow;
  208.     SetCursor(theCursors[normcurs]);
  209. }
  210.  
  211. /*     If a file with the filetype "DBUG" was sent to us, set the debug mode. */
  212. short checkdebug( void)
  213. {
  214.     short i,mess,count;
  215.     AppFile theFile;
  216.     long junk = 0;
  217.  
  218.     if (gAEavail) return(FALSE);    //     Don't use CountAppFiles if AppleEvents
  219.                                     //    are available.
  220. #ifndef __powerpc__    
  221.     CountAppFiles(&mess,&count);
  222.     if (mess==1) return(FALSE);
  223.     if (count<1) return(FALSE);
  224.     for(i=1;i<=count;i++) {
  225.         GetAppFiles(i, &theFile);
  226.         if (theFile.fType=='DBUG') {
  227.             ClrAppFiles(i);
  228.             return(TRUE);
  229.             }
  230.         }
  231.     return(FALSE);
  232. #endif
  233. }
  234.  
  235. void InquireEnvironment( void)
  236. {
  237.     OSErr    err;
  238.     Boolean HFSflag;
  239.     long    response;
  240.     
  241.     long *HFSp=(long *)1014L;
  242.     
  243.     HFSflag= (*HFSp) > 0L;
  244.     if (!HFSflag) FatalAlert(NEED_HFS_ERR, 0, 0);
  245.  
  246.     err = SysEnvirons(CurrentVersion, &theWorld);
  247.     
  248.     if (err == envVersTooBig) FatalAlert(SYS_ENVIRON_ERR, 0, 0);
  249.     
  250.     if (theWorld.systemVersion < 0x0600) FatalAlert(SYSTEM_VERS_ERR, 0, 0);
  251.     
  252.     if (theWorld.machineType < 0) FatalAlert(ROM_VERS_ERR, 0, 0);
  253.     
  254.     // If there is a problem w/Gestalt, assume our keyboard has a Control key.
  255.     // Otherwise, we assume we have a control key unless a Mac or MacPlus keyboard is
  256.     // present.
  257.         
  258.     if ((err = Gestalt(gestaltKeyboardType, &response)) != noErr)
  259.         gKeyboardHasControlKey = TRUE;
  260.     else
  261.         gKeyboardHasControlKey = !(    (response == gestaltMacKbd) ||
  262.                                     (response == gestaltMacAndPad) ||
  263.                                     (response == gestaltMacPlusKbd) );
  264.                                     
  265.     /* System 6.0 and up ALWAYS has WNE trap! */
  266. }
  267.  
  268. void initmac( void)
  269. {
  270.     EventRecord myEvent;
  271.     short     i;
  272.     long    gestaltvalue;
  273.     OSErr    err;
  274.  
  275.     InitGraf(&qd.thePort);
  276.     InitFonts();
  277.     InitWindows();
  278.     InitMenus();
  279.     TEInit();
  280.     InitDialogs(0L);                /* No resume proc */
  281.     InitCursor();
  282.     
  283.     TelInfo = (TelInfoRec *)NewPtrClear(sizeof(TelInfoRec));
  284.     mungbuf = (char *)NewPtrClear(1024);
  285.     tempspot = (char *)NewPtrClear(256);
  286.     gApplicationPrefs = (ApplicationPrefs *)NewPtrClear(sizeof(ApplicationPrefs));
  287.     gFTPServerPrefs = (FTPServerPrefs *)NewPtrClear(sizeof(FTPServerPrefs));
  288.     screens = (WindRec *)NewPtrClear(MaxSess*sizeof(WindRec));
  289.  
  290.     InquireEnvironment();
  291.  
  292.     for (i=1;i<12;i++)
  293.         EventAvail( 0xffff, &myEvent);
  294.     
  295.     err = Gestalt(gestaltAppleEventsAttr, &gestaltvalue);        // See if AppleEvents are available
  296.     gAEavail = (!err && ((gestaltvalue >> gestaltAppleEventsPresent) & 0x0001));
  297.     
  298.     if (gAEavail) {
  299.         if (err = AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
  300.                                             MyHandleOAppUPP,0,FALSE))
  301.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  302.         if (err = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
  303.                                             MyHandleODocUPP,0,FALSE))
  304.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  305.         if (err = AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,
  306.                                             MyHandlePDocUPP,0,FALSE))
  307.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  308.         if (err = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
  309.                                             MyHandleQuitUPP,0,FALSE))
  310.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  311.         }
  312.  
  313.     TelInfo->screenRect = qd.screenBits.bounds;            /* well, they have to be set */
  314.                                                 /* somewhere, where else ? */
  315.     SetRect(&TelInfo->dragRect, 4, 24, TelInfo->screenRect.right-4,
  316.                         TelInfo->screenRect.bottom-4);
  317. }
  318.  
  319. void DoTheGlobalInits(void)
  320. {
  321.     long junk = 0;
  322.     ParamBlockRec pb;
  323.     
  324.     TelInfo->ScrlLock=0;
  325.     TelInfo->ftplogon=0;
  326.     TelInfo->done=0;
  327.     TelInfo->MacBinary=0;
  328.     TelInfo->numwindows=0;
  329.     TelInfo->CONFstate=0;
  330.     TelInfo->CONFactive=0;
  331.     TelInfo->SettingsFile=-1;
  332.     TelInfo->ApplicationFile = CurResFile();
  333.     TelInfo->ginon   = 0;
  334.     TelInfo->xferon  = 0;
  335.     TelInfo->graphs = 0;
  336.     TelInfo->debug = 1;
  337.     TelInfo->myfronttype = NO_WINDOW;
  338.     TelInfo->myfrontvs = 0;
  339.     TelInfo->suspended = FALSE;
  340.     TelInfo->myfrontwindow = 0L;    
  341.     TelInfo->myfrontRgn = 0L;    
  342.     TelInfo->lastCursor = 0L;
  343.     TelInfo->DefaultDirVRefNum = -1;
  344.     TelInfo->FTPClientVRefNum = -1;
  345.     TelInfo->DefaultDirDirID = 2;
  346.     TelInfo->FTPClientDirID = 2;
  347.     
  348.     pb.ioParam.ioNamePtr = 0L;
  349.     PBGetVol(&pb, FALSE);            /* see Tech Note 140 for implications of this */
  350.  
  351.     GetWDInfo(pb.ioParam.ioVRefNum, &(TelInfo->ApFolder.vRefNum), &(TelInfo->ApFolder.parID), &junk);
  352.     TelInfo->ApFolder.name[0] = 0;
  353.  
  354.     FindFolder( kOnSystemDisk, kSystemFolderType, kCreateFolder,
  355.                     &(TelInfo->SysFolder.vRefNum), &(TelInfo->SysFolder.parID));
  356.     TelInfo->SysFolder.name[0]=0;
  357.  
  358.     FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  359.                     &(TelInfo->PrefFolder.vRefNum), &(TelInfo->PrefFolder.parID));
  360.     TelInfo->PrefFolder.name[0]=0;
  361.     
  362.     FindFolder( kOnSystemDisk, kTemporaryFolderType, kCreateFolder, &TempItemsVRefNum, &TempItemsDirID);
  363. }
  364.  
  365. void init (void)
  366. {
  367.     long        s;
  368.     DialogPtr     dtemp;        /* Used for dialog display */
  369.  
  370.     FlushEvents(everyEvent,0);
  371.     initmac();                /* initialize Macintosh stuff */
  372.     
  373.     DoTheGlobalInits();
  374.  
  375.     OpenPreferencesFile();
  376.     LoadPreferences();
  377.     UnloadSeg(&LoadPreferences);
  378.     
  379.     setupmenu(gApplicationPrefs->CommandKeys);
  380.  
  381.     dtemp = GetNewMyDialog(FirstDLOG, NULL, kInFront, (void *)ThirdCenterDialog);    /* opening dialog */
  382.     UItemAssign( dtemp, 2, VersionNumberUPP);
  383.     DrawDialog(dtemp);                                        /* while we init everything */
  384.  
  385.     trInit(myMenus[National]);    /* LU: Initialize translation routines; must be done */
  386.                                 /* LU: prior to checkdebug(), since even the debug      */
  387.                                 /* LU: output goes through the translation routines. */
  388.     UnloadSeg(&trInit);
  389.  
  390.     TelInfo->debug=checkdebug();        /* must return TRUE or FALSE */
  391.     
  392.     initmcurs(TelInfo->debug);        /* init the mouse cursor */
  393.     SetCursor(theCursors[watchcurs]);
  394.  
  395.     if (RGMalloc()) {        
  396.         DoError(103 | MEMORY_ERRORCLASS, LEVEL3, NULL);
  397.         forcequit();
  398.     }                        
  399.     UnloadSeg(&RGMalloc);
  400.     
  401.     VGalloc();                        /* BYU - save global space */
  402.     UnloadSeg(&VGalloc);
  403.     
  404.     if (VSinit(MaxSess+2)) {        /* initialize the Virtual Screen */
  405.         DoError(104 | MEMORY_ERRORCLASS, LEVEL3, NULL);
  406.         forcequit();
  407.     }
  408.     UnloadSeg(&VSinit);
  409.     
  410.     InitDebug();
  411.     initftplog();                    /* allocate/initialize ftp log screen */
  412.  
  413.     VGinit();                        /* initialize Virtual Graphics after VS so that */
  414.                                     /* init messages can come on console screen */
  415.     UnloadSeg(&VGinit);
  416.     
  417.     VRinit();
  418.     UnloadSeg(&VRinit);
  419.     
  420.     MacRGinit();
  421.     UnloadSeg(&MacRGinit);
  422.     
  423.     init_mb_files();                /* Initialize file id's to closed state */
  424.     UnloadSeg(init_mb_files);
  425.  
  426.     initnet();                        /* initialize network stuff */
  427.     switchMenus(gApplicationPrefs->CommandKeys);
  428.  
  429.     TelInfo->done = FALSE;            // We're not quitting yet.
  430.  
  431.     SetCursor(theCursors[normcurs]);
  432.  
  433.     initmacros();
  434. //    initEvents();
  435.  
  436.     io = OpenResolver(NULL);
  437.  
  438.     if (io) {                                
  439.         DialogPtr theErrDialog;
  440.         short itemhit = 0;
  441.  
  442.         theErrDialog = GetNewDialog(RESOLVERERROR, NULL, kInFront);
  443.         DrawDialog(theErrDialog);
  444.         while (!itemhit)
  445.             ModalDialog(DLOGwOKUPP,&itemhit);
  446.         DisposDialog(theErrDialog);
  447.         }
  448.     
  449.     updateMenuChecks();
  450.     DoTheMenuChecks();
  451.  
  452.     checkCONF();                     /* Did user click on a set? */
  453.  
  454.     DisposDialog(dtemp);            /* Remove the splashbox...  */
  455.  
  456.     encryptOK = authOK = true;
  457.     if (s = init_cornell_des()) {
  458.         encryptOK = false;
  459.         if (s == -2)                /* if no kdriver */
  460.             authOK = false;
  461.     }
  462.  
  463. }